﻿using System;
using System.Globalization;
using System.Reflection;

namespace WebCommon.Com
{
    /// <summary>
    /// 反射操作
    /// </summary>
    public class TypeCom
    {
        /// <summary>
        /// 反射获取字段名
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetPropertyName<T>(T obj, string str)
        {
        
            Type t = obj.GetType();

            foreach (PropertyInfo pi in t.GetProperties())
            {
                if (pi.PropertyType.Name.ToLower() == "string")
                {
                    object val = pi.GetValue(obj, null);//用pi.GetValue获得值

                    if ((string)val == str)
                    {
                        return pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                    }
                }
            }
            return string.Empty;
        }
    }

    /// <summary>
    /// 获取唯一ID
    /// </summary>
    public class OnlyId
    {
        /// <summary>
        /// The random.
        /// </summary>
        static Random random = new Random();

        /// <summary>
        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks(int x)
        {
            return DateTime.Now.Ticks + RandomStr(x);
        }

        /// <summary>
        /// 唯一时间(yymmdd)Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTime(int x)
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss") + RandomStr(x);
        }

        /// <summary>
        /// 唯一时间(yymmdd)Id
        /// </summary>
        /// <returns>
        /// </returns>
        public static string OnlyTime16()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss") + RandomStr(100);
        }

        /// <summary>
        /// 唯一时间(yymmdd)Id
        /// </summary>
        /// <returns>
        /// </returns>
        public static string OnlyTime20()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssff") + RandomStr(10000);
        }

        /// <summary>
        /// 随机数
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        static string RandomStr(int x)
        {
            int sta = x;
            int end = sta * 10 - 1;
            return random.Next(sta, end).ToString(CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks16()
        {
            return Static.GetTimeStamp() + RandomStr(100000);
        } 
        /// <summary>
        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks12()
        {
            return Static.GetTimeStamp() + RandomStr(10);
        } 

        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks8()
        {
            return (Static.GetTimeStamp(10000)) + RandomStr(10);
        }

        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks10()
        {
            return (Static.GetTimeStamp(1000)) + RandomStr(100);
        }
    }
}